<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="发布说明" tabindex="-1"><a class="header-anchor" href="#发布说明"><span>发布说明</span></a></h2>
<ul>
<li><a href="#versioning-scheme">版本方案</a></li>
<li><a href="#support-policy">支持政策</a></li>
<li><a href="#laravel-11">Laravel 11</a></li>
</ul>
<h2 id="版本方案" tabindex="-1"><a class="header-anchor" href="#版本方案"><span><a href="#versioning-scheme">版本方案</a></span></a></h2>
<p>Laravel 及其其他官方包遵循 <a href="https://semver.org/" target="_blank" rel="noopener noreferrer">语义化版本控制</a>。主要框架版本每年发布一次（大约在第一季度），而次要版本和补丁版本可能会每周发布一次。次要版本和补丁版本<strong>绝不会</strong>包含破坏性更改。</p>
<p>在应用程序或包中引用 Laravel 框架或其组件时，您应始终使用类似 <code v-pre>^11.0</code> 的版本约束，因为 Laravel 的主要版本确实包含破坏性更改。然而，我们始终努力确保您可以在一天或更短时间内更新到新的主要版本。</p>
<h4 id="命名参数" tabindex="-1"><a class="header-anchor" href="#命名参数"><span><a href="#named-arguments">命名参数</a></span></a></h4>
<p><a href="https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments" target="_blank" rel="noopener noreferrer">命名参数</a> 不在 Laravel 的向后兼容性指南范围内。我们可能会在必要时重命名函数参数，以改进 Laravel 代码库。因此，在调用 Laravel 方法时使用命名参数应谨慎进行，并理解参数名称在未来可能会发生变化。</p>
<h2 id="支持政策" tabindex="-1"><a class="header-anchor" href="#支持政策"><span><a href="#support-policy">支持政策</a></span></a></h2>
<p>对于所有 Laravel 版本，提供18个月的错误修复和2年的安全修复。对于所有附加库，包括 Lumen，只有最新的主要版本会接收错误修复。此外，请查看 <a href="https://learnku.com/docs/11.x/database#introduction" target="_blank" rel="noopener noreferrer">Laravel 支持的数据库版本</a>。</p>
<table>
<thead>
<tr>
<th>版本</th>
<th>PHP (*)</th>
<th>发布</th>
<th>错误修复截止</th>
<th>安全修复截止</th>
</tr>
</thead>
<tbody>
<tr>
<td>9</td>
<td>8.0 - 8.2</td>
<td>2022年2月8日</td>
<td>2023年8月8日</td>
<td>2024年2月6日</td>
</tr>
<tr>
<td>10</td>
<td>8.1 - 8.3</td>
<td>2023年2月14日</td>
<td>2024年8月6日</td>
<td>2025年2月4日</td>
</tr>
<tr>
<td>11</td>
<td>8.2 - 8.3</td>
<td>2024年3月12日</td>
<td>2025年9月3日</td>
<td>2026年3月12日</td>
</tr>
<tr>
<td>12</td>
<td>8.2 - 8.3</td>
<td>2025年第一季度</td>
<td>2026年第三季度</td>
<td>2027年第一季度</td>
</tr>
</tbody>
</table>
<p>终止支持</p>
<p>仅安全修复</p>
<p>(*) 支持的 PHP 版本</p>
<h2 id="laravel-11" tabindex="-1"><a class="header-anchor" href="#laravel-11"><span><a href="#laravel-11">Laravel 11</a></span></a></h2>
<p>Laravel 11 继续改进 Laravel 10.x，通过引入简化的应用程序结构、每秒速率限制、健康路由、优雅的加密密钥轮换、队列测试改进、<a href="https://resend.com/" target="_blank" rel="noopener noreferrer">Resend</a> 邮件传输、Prompt 验证器集成、新的 Artisan 命令等。此外，Laravel Reverb，一个官方的可扩展 WebSocket 服务器也被引入，为您的应用程序提供强大的实时功能。</p>
<h3 id="php-8-2" tabindex="-1"><a class="header-anchor" href="#php-8-2"><span><a href="#php-8">PHP 8.2</a></span></a></h3>
<p>Laravel 11.x 要求最低 PHP 版本为 8.2。</p>
<h3 id="简化的应用程序结构" tabindex="-1"><a class="header-anchor" href="#简化的应用程序结构"><span><a href="#structure">简化的应用程序结构</a></span></a></h3>
<p><em>Laravel 的简化应用程序结构由 <a href="https://github.com/taylorotwell" target="_blank" rel="noopener noreferrer">Taylor Otwell</a> 和 <a href="https://github.com/nunomaduro" target="_blank" rel="noopener noreferrer">Nuno Maduro</a> 开发</em>。</p>
<p>Laravel 11 为<strong>新</strong>的 Laravel 应用程序引入了简化的应用程序结构，无需对现有应用程序进行任何更改。新的应用程序结构旨在提供更简洁、更现代的体验，同时保留 Laravel 开发者已经熟悉的许多概念。下面我们将讨论 Laravel 新应用程序结构的亮点。</p>
<h4 id="应用程序引导文件" tabindex="-1"><a class="header-anchor" href="#应用程序引导文件"><span>应用程序引导文件</span></a></h4>
<p><code v-pre>bootstrap/app.php</code> 文件已被重新设计为一个代码优先的应用程序配置文件。从这个文件中，您现在可以自定义应用程序的路由、中间件、服务提供者、异常处理等。这个文件统一了之前分散在应用程序文件结构中的各种高级应用程序行为设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Application</span><span class="token operator">::</span><span class="token function">configure</span><span class="token punctuation">(</span><span class="token argument-name">basePath</span><span class="token punctuation">:</span> <span class="token function">dirname</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">withRouting</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">web</span><span class="token punctuation">:</span> <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../routes/web.php'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">commands</span><span class="token punctuation">:</span> <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../routes/console.php'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">health</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'/up'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">withMiddleware</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Middleware</span> <span class="token variable">$middleware</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">withExceptions</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Exceptions</span> <span class="token variable">$exceptions</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="服务提供者" tabindex="-1"><a class="header-anchor" href="#服务提供者"><span><a href="#service-providers">服务提供者</a></span></a></h4>
<p>默认的 Laravel 应用程序结构包含五个服务提供者，而 Laravel 11 仅包含一个 <code v-pre>AppServiceProvider</code>。之前服务提供者的功能已被整合到 <code v-pre>bootstrap/app.php</code> 中，自动由框架处理，或可以放置在应用程序的 <code v-pre>AppServiceProvider</code> 中。</p>
<p>例如，事件发现现在默认启用，基本上消除了手动注册事件及其监听器的需求。然而，如果您确实需要手动注册事件，您可以简单地在 <code v-pre>AppServiceProvider</code> 中进行。同样，之前在 <code v-pre>AuthServiceProvider</code> 中注册的路由模型绑定或授权门也可以在 <code v-pre>AppServiceProvider</code> 中注册。</p>
<h4 id="选择性-api-和广播路由" tabindex="-1"><a class="header-anchor" href="#选择性-api-和广播路由"><span><a href="#opt-in-routing">选择性 API 和广播路由</a></span></a></h4>
<p>默认情况下，不再包含 <code v-pre>api.php</code> 和 <code v-pre>channels.php</code> 路由文件，因为许多应用程序不需要这些文件。相反，它们可以通过简单的 Artisan 命令创建：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan install:api</span>
<span class="line">php artisan install:broadcasting</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="中间件" tabindex="-1"><a class="header-anchor" href="#中间件"><span><a href="#middleware">中间件</a></span></a></h4>
<p>以前，新的 Laravel 应用程序包含九个中间件。这些中间件执行各种任务，例如验证请求、修剪输入字符串和验证 CSRF 令牌。</p>
<p>在 Laravel 11 中，这些中间件已被移入框架本身，因此它们不会增加应用程序结构的负担。用于自定义这些中间件行为的新方法已添加到框架中，并可以从应用程序的 <code v-pre>bootstrap/app.php</code> 文件中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">-></span><span class="token function">withMiddleware</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Middleware</span> <span class="token variable">$middleware</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$middleware</span><span class="token operator">-></span><span class="token function">validateCsrfTokens</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">except</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'stripe/*'</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$middleware</span><span class="token operator">-></span><span class="token function">web</span><span class="token punctuation">(</span><span class="token argument-name">append</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">EnsureUserIsSubscribed</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于所有中间件都可以通过应用程序的 <code v-pre>bootstrap/app.php</code> 轻松自定义，因此不再需要单独的 HTTP “kernel” 类。</p>
<h4 id="任务调度" tabindex="-1"><a class="header-anchor" href="#任务调度"><span><a href="#scheduling">任务调度</a></span></a></h4>
<p>通过新的 <code v-pre>Schedule</code> facade，现在可以直接在应用程序的 <code v-pre>routes/console.php</code> 文件中定义计划任务，从而无需单独的控制台 “kernel” 类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schedule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schedule</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails:send'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">daily</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="异常处理" tabindex="-1"><a class="header-anchor" href="#异常处理"><span><a href="#exception-handling">异常处理</a></span></a></h4>
<p>与路由和中间件类似，现在可以从应用程序的 <code v-pre>bootstrap/app.php</code> 文件中自定义异常处理，而不是使用单独的异常处理类，从而减少新 Laravel 应用程序中的文件数量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">-></span><span class="token function">withExceptions</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Exceptions</span> <span class="token variable">$exceptions</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$exceptions</span><span class="token operator">-></span><span class="token function">dontReport</span><span class="token punctuation">(</span><span class="token class-name static-context">MissedFlightException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$exceptions</span><span class="token operator">-></span><span class="token function">report</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">InvalidOrderException</span> <span class="token variable">$e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="基础-controller-类" tabindex="-1"><a class="header-anchor" href="#基础-controller-类"><span><a href="#base-controller-class">基础 <code v-pre>Controller</code> 类</a></span></a></h4>
<p>新 Laravel 应用程序中包含的基础控制器已被简化。它不再继承 Laravel 的内部 <code v-pre>Controller</code> 类，并且 <code v-pre>AuthorizesRequests</code> 和 <code v-pre>ValidatesRequests</code> traits 也被移除，因为它们可以在需要时包含在应用程序的各个控制器中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name-definition class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="应用程序默认设置" tabindex="-1"><a class="header-anchor" href="#应用程序默认设置"><span><a href="#application-defaults">应用程序默认设置</a></span></a></h4>
<p>默认情况下，新 Laravel 应用程序使用 SQLite 进行数据库存储，并使用 <code v-pre>database</code> 驱动来处理 Laravel 的会话、缓存和队列。这允许您在创建新 Laravel 应用程序后立即开始构建应用程序，而无需安装额外的软件或创建额外的数据库迁移。</p>
<p>此外，随着时间的推移，这些 Laravel 服务的 <code v-pre>database</code> 驱动已经变得足够强大，可以在许多应用程序上下文中用于生产环境；因此，它们为本地和生产应用程序提供了一个合理的、统一的选择。</p>
<h3 id="laravel-reverb" tabindex="-1"><a class="header-anchor" href="#laravel-reverb"><span><a href="#reverb">Laravel Reverb</a></span></a></h3>
<p><em>Laravel Reverb 由 <a href="https://github.com/joedixon" target="_blank" rel="noopener noreferrer">Joe Dixon</a> 开发</em>。</p>
<p><a href="https://reverb.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Reverb</a> 为您的 Laravel 应用程序带来了极速且可扩展的实时 WebSocket 通信，并与 Laravel 现有的事件广播工具（如 Laravel Echo）无缝集成。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan reverb:start</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此外，Reverb 支持通过 Redis 的发布/订阅功能进行水平扩展，使您能够将 WebSocket 流量分布在多个后端 Reverb 服务器上，从而支持单个高需求应用程序。</p>
<p>有关 Laravel Reverb 的更多信息，请查阅完整的 <a href="https://learnku.com/docs/11.x/reverb" target="_blank" rel="noopener noreferrer">Reverb 文档</a>。</p>
<h3 id="每秒速率限制" tabindex="-1"><a class="header-anchor" href="#每秒速率限制"><span><a href="#rate-limiting">每秒速率限制</a></span></a></h3>
<p><em>每秒速率限制由 <a href="https://github.com/timacdonald" target="_blank" rel="noopener noreferrer">Tim MacDonald</a> 贡献。</em></p>
<p>Laravel 现在支持所有速率限制器的“每秒”速率限制，包括 HTTP 请求和队列作业的速率限制。之前，Laravel 的速率限制器仅限于“每分钟”的粒度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">RateLimiter</span><span class="token operator">::</span><span class="token function">for</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'invoices'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Limit</span><span class="token operator">::</span><span class="token function">perSecond</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有关 Laravel 中速率限制的更多信息，请查看 <a href="https://learnku.com/docs/11.x/routing#rate-limiting" target="_blank" rel="noopener noreferrer">速率限制文档</a>。</p>
<h3 id="健康检查路由" tabindex="-1"><a class="header-anchor" href="#健康检查路由"><span><a href="#health">健康检查路由</a></span></a></h3>
<p><em>健康检查路由由 <a href="https://github.com/taylorotwell" target="_blank" rel="noopener noreferrer">Taylor Otwell</a> 贡献。</em></p>
<p>新的 Laravel 11 应用程序包括一个 <code v-pre>health</code> 路由指令，该指令指示 Laravel 定义一个简单的健康检查端点，供第三方应用程序健康监控服务或编排系统（如 Kubernetes）调用。默认情况下，该路由位于 <code v-pre>/up</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">-></span><span class="token function">withRouting</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">web</span><span class="token punctuation">:</span> <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../routes/web.php'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">commands</span><span class="token punctuation">:</span> <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../routes/console.php'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">health</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'/up'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当 HTTP 请求发到这个路由时，Laravel 还会调度一个 <code v-pre>DiagnosingHealth</code> 事件，使您能够执行与应用程序相关的额外健康检查。</p>
<h3 id="平滑的加密密钥轮换" tabindex="-1"><a class="header-anchor" href="#平滑的加密密钥轮换"><span><a href="#encryption">平滑的加密密钥轮换</a></span></a></h3>
<p><em>平滑的加密密钥轮换由 <a href="https://github.com/taylorotwell" target="_blank" rel="noopener noreferrer">Taylor Otwell</a> 贡献。</em></p>
<p>由于 Laravel 加密了所有的 cookies，包括应用程序的会话 cookie，因此基本上每个对 Laravel 应用程序的请求都依赖于加密。然而，正因为如此，轮换应用程序的加密密钥会使所有用户退出登录。此外，使用先前加密密钥加密的数据也将无法解密。</p>
<p>Laravel 11 允许您通过 <code v-pre>APP_PREVIOUS_KEYS</code> 环境变量以逗号分隔的列表形式定义应用程序的先前加密密钥。</p>
<p>在加密值时，Laravel 将始终使用 <code v-pre>APP_KEY</code> 环境变量中的“当前”加密密钥。在解密值时，Laravel 会首先尝试当前密钥。如果使用当前密钥解密失败，Laravel 将尝试所有先前的密钥，直到其中一个密钥能够成功解密该值。</p>
<p>这种平滑的解密方法允许用户在加密密钥轮换时继续不间断地使用您的应用程序。</p>
<p>有关 Laravel 中加密的更多信息，请查看 <a href="https://learnku.com/docs/11.x/encryption" target="_blank" rel="noopener noreferrer">加密文档</a>。</p>
<h3 id="自动密码重新哈希" tabindex="-1"><a class="header-anchor" href="#自动密码重新哈希"><span><a href="#automatic-password-rehashing">自动密码重新哈希</a></span></a></h3>
<p><em>自动密码重新哈希由 <a href="https://github.com/valorin" target="_blank" rel="noopener noreferrer">Stephen Rees-Carter</a> 贡献。</em></p>
<p>Laravel 的默认密码哈希算法是 bcrypt。bcrypt 哈希的“工作因子”可以通过 <code v-pre>config/hashing.php</code> 配置文件或 <code v-pre>BCRYPT_ROUNDS</code> 环境变量进行调整。</p>
<p>通常，随着 CPU / GPU 处理能力的提高，bcrypt 工作因子应逐渐增加。如果您为应用程序增加了 bcrypt 工作因子，Laravel 现在将优雅地、自动地在用户通过应用程序进行身份验证时重新哈希用户密码。</p>
<h3 id="提示验证" tabindex="-1"><a class="header-anchor" href="#提示验证"><span><a href="#prompt-validation">提示验证</a></span></a></h3>
<p><em>提示验证集成由 <a href="https://github.com/cerbero90" target="_blank" rel="noopener noreferrer">Andrea Marco Sartori</a> 贡献。</em></p>
<p><a href="https://learnku.com/docs/11.x/prompts" target="_blank" rel="noopener noreferrer">Laravel Prompts</a> 是一个 PHP 包，用于向您的命令行应用程序添加美观且用户友好的表单，具有包括占位符文本和验证在内的类似浏览器的功能。</p>
<p>Laravel Prompts 支持通过闭包进行输入验证：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">text</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">match</span> <span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">3</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The name must be at least 3 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">255</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The name must not exceed 255 characters.'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">default</span> <span class="token operator">=></span> <span class="token constant">null</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然而，当处理许多输入或复杂的验证场景时，这可能会变得繁琐。因此，在 Laravel 11 中，您可以在验证提示输入时利用 Laravel 的 <a href="https://learnku.com/docs/11.x/validation" target="_blank" rel="noopener noreferrer">validator</a> 的全部功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token argument-name">validate</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|min:3|max:255'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="队列交互测试" tabindex="-1"><a class="header-anchor" href="#队列交互测试"><span><a href="#queue-interaction-testing">队列交互测试</a></span></a></h3>
<p><em>队列交互测试由 <a href="https://github.com/taylorotwell" target="_blank" rel="noopener noreferrer">Taylor Otwell</a> 贡献。</em></p>
<p>之前，尝试测试一个队列作业是否被释放、删除或手动失败是非常繁琐的，并且需要定义自定义的队列伪造和存根。然而，在 Laravel 11 中，您可以使用 <code v-pre>withFakeQueueInteractions</code> 方法轻松测试这些队列交互：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>ProcessPodcast</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$job</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ProcessPodcast</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withFakeQueueInteractions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$job</span><span class="token operator">-></span><span class="token function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$job</span><span class="token operator">-></span><span class="token function">assertReleased</span><span class="token punctuation">(</span><span class="token argument-name">delay</span><span class="token punctuation">:</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有关测试队列作业的更多信息，请查看 <a href="https://learnku.com/docs/11.x/queues#testing" target="_blank" rel="noopener noreferrer">队列文档</a>。</p>
<h3 id="新的-artisan-命令" tabindex="-1"><a class="header-anchor" href="#新的-artisan-命令"><span><a href="#new-artisan-commands">新的 Artisan 命令</a></span></a></h3>
<p><em>类创建 Artisan 命令由 <a href="https://github.com/taylorotwell" target="_blank" rel="noopener noreferrer">Taylor Otwell</a> 贡献。</em></p>
<p>新增了一些 Artisan 命令，允许快速创建类、枚举、接口和特性：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:class</span>
<span class="line">php artisan make:enum</span>
<span class="line">php artisan make:interface</span>
<span class="line">php artisan make:trait</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="模型转换改进" tabindex="-1"><a class="header-anchor" href="#模型转换改进"><span><a href="#model-cast-improvements">模型转换改进</a></span></a></h3>
<p><em>模型转换改进由 <a href="https://github.com/nunomaduro" target="_blank" rel="noopener noreferrer">Nuno Maduro</a> 贡献。</em></p>
<p>Laravel 11 支持使用方法而不是属性来定义模型的转换。这使得转换定义更加简洁流畅，特别是在使用带参数的转换时：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取应转换的属性。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">casts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token class-name static-context">AsCollection</span><span class="token operator">::</span><span class="token function">using</span><span class="token punctuation">(</span><span class="token class-name static-context">OptionCollection</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// 'options' => AsEncryptedCollection::using(OptionCollection::class),</span></span>
<span class="line">        <span class="token comment">// 'options' => AsEnumArrayObject::using(OptionEnum::class),</span></span>
<span class="line">        <span class="token comment">// 'options' => AsEnumCollection::using(OptionEnum::class),</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有关属性转换的更多信息，请查阅 <a href="https://learnku.com/docs/11.x/eloquent-mutators#attribute-casting" target="_blank" rel="noopener noreferrer">Eloquent 文档</a>。</p>
<h3 id="一次性函数-once" tabindex="-1"><a class="header-anchor" href="#一次性函数-once"><span><a href="#the-once-function">一次性函数 <code v-pre>once</code></a></span></a></h3>
<p><em><code v-pre>once</code> 助手函数由 <a href="https://github.com/taylorotwell" target="_blank" rel="noopener noreferrer">Taylor Otwell</a> 和 <a href="https://github.com/nunomaduro" target="_blank" rel="noopener noreferrer">Nuno Maduro</a> 贡献。</em></p>
<p><code v-pre>once</code> 助手函数执行给定的回调，并在请求期间将结果缓存到内存中。任何后续对 <code v-pre>once</code> 函数的相同回调调用都将返回先前缓存的结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">function</span> <span class="token function-definition function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">int</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">once</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">random_int</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 123</span></span>
<span class="line"><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 123 (缓存结果)</span></span>
<span class="line"><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 123 (缓存结果)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有关 <code v-pre>once</code> 助手的更多信息，请查看 <a href="https://learnku.com/docs/11.x/helpers#method-once" target="_blank" rel="noopener noreferrer">助手文档</a>。</p>
<h3 id="使用内存数据库进行测试时的性能提升" tabindex="-1"><a class="header-anchor" href="#使用内存数据库进行测试时的性能提升"><span><a href="#database-performance">使用内存数据库进行测试时的性能提升</a></span></a></h3>
<p><em>内存数据库测试性能提升由 <a href="https://github.com/AJenbo" target="_blank" rel="noopener noreferrer">Anders Jenbo</a> 贡献。</em></p>
<p>在使用 <code v-pre>：memory：</code>(注意:此处冒号应为英文，但英文符号会触发表情导致显示错误故改为中文冒号！！！) SQLite 数据库进行测试时，Laravel 11 提供了显著的速度提升。为此，Laravel 现在维护对 PHP 的 PDO 对象的引用，并在连接之间重用它，这通常会将总测试运行时间减半。</p>
<h3 id="对-mariadb-的改进支持" tabindex="-1"><a class="header-anchor" href="#对-mariadb-的改进支持"><span><a href="#mariadb">对 MariaDB 的改进支持</a></span></a></h3>
<p><em>对 MariaDB 的改进支持由 <a href="https://github.com/staudenmeir" target="_blank" rel="noopener noreferrer">Jonas Staudenmeir</a> 和 <a href="https://github.com/Jubeki" target="_blank" rel="noopener noreferrer">Julius Kiekbusch</a> 贡献。</em></p>
<p>Laravel 11 包含对 MariaDB 的改进支持。在以前的 Laravel 版本中，您可以通过 Laravel 的 MySQL 驱动程序使用 MariaDB。然而，Laravel 11 现在包含一个专用的 MariaDB 驱动程序，为此数据库系统提供了更好的默认设置。</p>
<p>有关 Laravel 数据库驱动程序的更多信息，请查看 <a href="https://learnku.com/docs/11.x/database" target="_blank" rel="noopener noreferrer">数据库文档</a>。</p>
<h3 id="数据库检查和改进的模式操作" tabindex="-1"><a class="header-anchor" href="#数据库检查和改进的模式操作"><span><a href="#inspecting-database">数据库检查和改进的模式操作</a></span></a></h3>
<p><em>改进的模式操作和数据库检查由 <a href="https://github.com/hafezdivandari" target="_blank" rel="noopener noreferrer">Hafez Divandari</a> 贡献。</em></p>
<p>Laravel 11 提供了额外的数据库模式操作和检查方法，包括原生修改、重命名和删除列。此外，还提供了高级空间类型、非默认模式名称以及用于操作表、视图、列、索引和外键的原生模式方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$tables</span> <span class="token operator">=</span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">getTables</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$views</span> <span class="token operator">=</span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">getViews</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$columns</span> <span class="token operator">=</span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">getColumns</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$indexes</span> <span class="token operator">=</span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">getIndexes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$foreignKeys</span> <span class="token operator">=</span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">getForeignKeys</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/releasesmd/16644" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/re...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/releasesmd/16644" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/re...</a></p>
</blockquote>
</div></template>


